﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MusicPlay : System.Web.UI.Page
{
    public static string FilePath { get; set; }
    static int mid;

    protected void Page_Load(object sender, EventArgs e)
    {
        UserLogin user = (UserLogin)Session["User"];

        if (!IsPostBack)
        {
            if (user != null)
            {
                LoadPlaylist(user);
            }
        }

        string mid_s = Request.QueryString["mid"];
        bool t = Int32.TryParse(mid_s, out mid);
        if (t)
        {
            Sharemp3DataContext db = new Sharemp3DataContext();

            var selectedMusic = (from i in db.Musics
                                 where i.MusicID == mid
                                 select new
                                 {
                                     MusicID = i.MusicID,
                                     FilePath = i.FilePath,
                                     OGA = i.OGA,
                                     Title = i.Title,
                                     AlbumID = i.AlbumID,
                                     AlbumName = i.Album.AlbumName,
                                     ArtistID = i.Album.ArtistID,
                                     ArtistName = i.Album.Artist.ArtistName,
                                     Description = i.Album.Artist.Description,
                                     ImageCover = i.Album.Artist.ImageCover,
                                     GenresID = i.GenresID,
                                     GenresName = i.Genre.GenresName,
                                     Rating = i.Rating,
                                     Buy = i.Buy,
                                     Price = i.Price,
                                     Duration = i.Duration,
                                     Cover = i.Cover,
                                     Date = i.Album.Date
                                 }).First();

            titleDetails.InnerText = selectedMusic.Title + " - " + selectedMusic.ArtistName;
            musicTittle.InnerText = selectedMusic.Title + " - " + selectedMusic.Date.Value.Year;

            artistlnk.InnerText = selectedMusic.ArtistName;
            artistlnk.HRef = "ArtistMusic.aspx?aid=" + selectedMusic.ArtistID;

            albumlnk.InnerText = "(" + selectedMusic.AlbumName + ")";
            albumlnk.HRef = "TrackList.aspx?alid=" + selectedMusic.AlbumID;

            musicCover.Src = selectedMusic.ImageCover;
            musicCover.Alt = artistname.InnerText = selectedMusic.ArtistName;
            artistname.HRef = "ArtistMusic.aspx?aid=" + selectedMusic.ArtistID;
            artistdescription.InnerText = selectedMusic.Description;
            FilePath = selectedMusic.FilePath;

            var album = db.Albums.Where(i => i.ArtistID == selectedMusic.ArtistID).Take(10);
            Rpt_AlbumArtist.DataSource = album;
            Rpt_AlbumArtist.DataBind();
        }
    }

    private void LoadPlaylist(UserLogin user)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();
        int userid = user.UserID;

        var playlist = from i in db.Playlists
                       where i.UserID == userid
                       orderby i.CreateDate descending
                       select new
                       {
                           PlaylistID = i.PlaylistID,
                           UserID = i.UserID,
                           Title = i.Title,
                           NumMusic = i.PlaylistMusics.Count(),
                           CreateDate = i.CreateDate
                       };
        Lst_Playlist.DataSource = playlist;
        Lst_Playlist.DataValueField = "PlaylistID";
        Lst_Playlist.DataTextField = "Title";
        Lst_Playlist.DataBind();
    }

    protected void OpenAddPlaylistPanel(object sender, EventArgs e)
    {
        UserLogin user = (UserLogin)Session["User"];
        if (user != null)
        {
            if (lnkAddToPlaylist.Text.Equals("Add To Playlist"))
            {
                AddToPlaylistPanel.Style["display"] = "block";
                lnkAddToPlaylist.Text = "Cancel";
            }
            else
            {
                AddToPlaylistPanel.Style["display"] = "none";
                lnkAddToPlaylist.Text = "Add To Playlist";
            }
        }
        else
        {
            Response.Redirect("UserLogin.aspx");
        }
    }

    protected void AddMusicToPlaylist(object sender, EventArgs e)
    {
        int playlistid = Convert.ToInt32(Lst_Playlist.SelectedValue);
        using (Sharemp3DataContext db = new Sharemp3DataContext())
        {
            PlaylistMusic mp = new PlaylistMusic() { PlaylistID = playlistid, MusicID = mid };
            try
            {
                db.PlaylistMusics.InsertOnSubmit(mp);
                db.SubmitChanges();
                lblAddResult.Text = "added !";
            }
            catch
            {
                lblAddResult.Text = "existed !";
            }
        }
        AddToPlaylistPanel.Style["display"] = "block";
        lnkAddToPlaylist.Text = "Cancel";
    }
}